/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package core;

import com.sun.lwuit.Dialog;
import com.sun.lwuit.Display;
import com.sun.lwuit.Image;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import view.FunctionMenu;
import view.ViewManager;
/**
 *
 * @author Kvest
 */
public class TimerService
{
    private static TimerService timerService;
    private static final Object lockObject = new Object();

    private Image connectionOn;
    private Image connectionOff;

    private long startTime;
    private int offset;
    private boolean countdownEnable;
    private int timeFixer;
    private int interfaceType;
    private String driverPositionText;

    public static TimerService getTimerService()
    {
        synchronized(lockObject)
        {
            if (timerService == null)
            {
                timerService = new TimerService();
            }
        }

        return timerService;
    }

    private TimerService()
    {
        startTime = 0;
        offset = 0;
        countdownEnable = false;
        timeFixer = Settings.getSettings().getTimeFixer();
        interfaceType = ViewManager.getViewManager().getFunctionMenuType();
        driverPositionText = null;
    }

    public void setTimeFixer(int timeFixer)
    {
        this.timeFixer = timeFixer;
    }

    public void start()
    {
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask()
        {
            public void run()
            {
                if (Display.getInstance().getCurrent() instanceof Dialog)
                {
                    return;
                }
                
                if (interfaceType == FunctionMenu.KIROVOGRAD_TYPE) { 
                    if (driverPositionText != null) {
                        Display.getInstance().getCurrent().setTitle(driverPositionText);
                        //Показываем наличие соединения
                        setConnectionState();
                    } else {
                        addTimeAndConnectionStateToTitle();
                    }
                } else {
                    addTimeAndConnectionStateToTitle();
                }
                
            }
        }, 500, 500);
    }

    private void setConnectionState() {
        if (MainOne.getMainOne().isConnected())
        {
            Display.getInstance().getCurrent().setTitleImage(connectionOn);
        }
        else
        {
            Display.getInstance().getCurrent().setTitleImage(connectionOff);
        }
    }
    
    private void addTimeAndConnectionStateToTitle() {
        //Показываем время
        String title = Display.getInstance().getCurrent().getTitleText();
        title = " " + timeToString() + " " + getCountdownTimerPart() + title;
        Display.getInstance().getCurrent().setTitle(title);

        //Показываем наличие соединения
        setConnectionState();
    }            
                
    public void addOffset(int delta)
    {
        offset += delta;
    }
    
    private String getCountdownTimerPart()
    {
        String result = "";
        if (countdownEnable)
        {
            long delta = (System.currentTimeMillis() - startTime - offset) / 1000;
            String sign = "";
            if (delta < 0)
            {
                delta *= -1;
                sign = "-";
            }
            long minutes = delta / 60;
            long seconds = delta % 60;
            result = "[" + sign + Long.toString(minutes) + ":" + (seconds < 10 ? "0" : "") + Long.toString(seconds) + "] ";
        }

        return result;
    }

    public void startTimer(int offset)
    {
        startTime = System.currentTimeMillis();
        this.offset = offset;
        countdownEnable = true;
    }

    public long stopTimer()
    {
        if (!countdownEnable)
        {
            return -1;
        }

        countdownEnable = false;
        long result = System.currentTimeMillis() - startTime - offset;
        startTime = 0;
        offset = 0;        
        return result;
    }

    private String timeToString()
    {
        Date date = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int m = c.get(Calendar.MINUTE) + (timeFixer % 60);
        int tmp = m / 60;
        m %= 60;
        int h = (c.get(Calendar.HOUR_OF_DAY) + (timeFixer / 60) + tmp) % 24;
        
        String t = (h < 10 ? "0" : "") + h + ":" + (m < 10 ? "0" : "") + m;
        return t;
    }

    public void setConnectionOff(Image connectionOff)
    {
        this.connectionOff = connectionOff;
    }

    public void setConnectionOn(Image connectionOn)
    {
        this.connectionOn = connectionOn;
    }
    
    public void setDriverPositionInfo(String numberInQueue,String positionName) {
        driverPositionText = "[" + numberInQueue + "] " + positionName;
    }
    
    public void resetDriverPositionInfo() {
        driverPositionText = null;
    }
}
